
var mongodb		= require('mongodb')
,	ua_parser	= require('ua-parser')
,	url			= require('url');



var get_location = function (req) {

	if (req.query && req.query.loc) {
		return req.query.loc;
	}

	if (req.headers.referer) {
		var ref = url.parse(req.headers.referer)
		return ref.protocol + '//' + ref.hostname + ref.pathname;
	}

	return '';
}



var get_page_data = function (req, client, callback) {

	if (req.query.p) {

		var collection	= new mongodb.Collection(client, 'pages')
		,	found		= false;

		collection.find({id: req.query.p}, function (err, cursor) {

			cursor.each(function (err, doc) {

				if (doc || err) {

					found = true;
					callback && callback(err, doc);

				} else if (found === false) {

					var doc = {
						loc: 	get_location(req),
						id: 	req.query.p,
						canvas:	req.query.cv || 0
					}

					collection.insert(doc, {}, function () {
						callback(null, doc);
					});

				}

			});

		});

	} else {
		callback(null, null);
	}

};


var get_visitor_data = function (req) {

	// user_id|page_id|first_visit|last_visit|this_visit|last_hit|visit_count
	var tokens = req.query.cc.split('|');

	if (tokens.length === 7) {

		var doc = {
			user_id:		parseInt(tokens[0]),
			first_visit:	parseInt(tokens[2]),
			last_visit:		parseInt(tokens[3]),
			current_visit:	parseInt(tokens[4]),
			last_hit:		parseInt(tokens[5]),
			visit_count:	parseInt(tokens[6])
		}

		if (req.query.ua) {

			var ua = ua_parser.parse(req.query.ua);

			doc.ua = {
				full:			ua.toFullString(),
				version:		ua.toVersionString(),
				browser:		ua.toString(),
				family:			ua.family,
				major:			ua.major,
				minor:			ua.minor,
				os:				ua.os
			}
		}

		return doc;
	}

	return {};
}


var get_session_data = function (req, client, callback) {

	// This requires both a page ID and a session ID
	if (!req.query.p || ! req.query.sn) {
		callback(null, null);
	}

	var user_id				= parseInt(req.query.u)
	,	page_id				= parseInt(req.query.p)
	,	collection_name		= 'clctr_' + page_id + '_sessions'
	,	collection			= new mongodb.Collection(client, collection_name)
	,	doc					= null
	;

	if (req.query.cc) {
		callback(null, get_visitor_data(req));
	} else {
		callback(null, {user_id: user_id, events: true});
	}
};









var finalize = function (result, callback) {
	// Check if both are ready
	if (result.page !== undefined && result.session !== undefined) {
		callback(result);
	}
};



exports.all = function (req, client, callback) {

	var result = {};

	get_page_data(req, client, function (err, page) {
		result.page = page;
		finalize(result, callback);
	});

	get_session_data(req, client, function (err, session) {
		result.session = session;
		finalize(result, callback);
	});
}

exports.page = function (req, client, callback) {

	var result = {session: null};

	get_page_data(req, client, function (err, page) {
		result.page = page;
		finalize(result, callback);
	});

}

exports.session = function (req, client, callback) {

	var result = {page: null};

	get_session_data(req, client, function (err, session) {
		result.session = session;
		finalize(result, callback);
	});

}












